package com.example.emergencyattempt01.activity.administrator_activity;

import android.os.Handler;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import com.example.emergencyattempt01.R;
import com.example.emergencyattempt01.activity.utils.AbstractNetWorkUtils;
import com.example.emergencyattempt01.activity.utils.NetWorkUtils;
import com.example.emergencyattempt01.activity.utils.ServerMessage;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;

public class Update_Book_Activity extends AppCompatActivity {
EditText bookNameField;
TextView ISBNField;
EditText authorField;
EditText pressField;
TextView sumNumberField;
TextView nowNumberField;
EditText typeField;
EditText introductionField;

Button confirm;
Button cancel;
Button addStoke;

AlertDialog alertDialog;

    String ISBN;
    String bookName;
    String author;
    String press;
    long sumNumber;
    long nowNumber;
    String type;
    String introduction;

    String toServer;

    Handler handlerForUpdateBook=new Handler()
    {
        @Override
        public void handleMessage(@NonNull Message msg) {
            switch (msg.what)
            {
                case UPDATE_INFO_SUCCESS:
                    Toast.makeText(Update_Book_Activity.this,"修改成功",Toast.LENGTH_SHORT).show();
                    finish();
                    break;
                case UPDATE_INFO_FAIL:
                    Toast.makeText(Update_Book_Activity.this,"修改失败",Toast.LENGTH_SHORT).show();
                    break;
                case UPDATE_STOKE_SUCCESS:
                    Toast.makeText(Update_Book_Activity.this,"修改成功",Toast.LENGTH_SHORT).show();
                    alertDialog.dismiss();
                    break;
                case UPDATE_STOKE_FAIL:
                    Toast.makeText(Update_Book_Activity.this,"修改失败",Toast.LENGTH_SHORT).show();
                    break;
            }
        }
    };
    final int UPDATE_INFO_SUCCESS=0x031;
    final int UPDATE_INFO_FAIL=0x032;
    final int UPDATE_STOKE_SUCCESS=0x033;
    final int UPDATE_STOKE_FAIL=0x034;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_update__book_);
        initPage();
        // 设置取消
        setCancel();
        // 设置确定
        setConfirm();
        // 修改库存
        updateStoke();
    }

    private void setConfirm() {
        confirm.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // 收集数据
                collectMessage();
                // 打包数据
                packMessage();
                // 上传数据
                updateMessage();
            }
        });
    }

    private void updateStoke() {
        addStoke.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                AlertDialog.Builder builder=new AlertDialog.Builder(Update_Book_Activity.this);
                View view= LayoutInflater.from(Update_Book_Activity.this).inflate(R.layout.update_stoke_dialog_layout,null);

                final EditText updateNmuberField=view.findViewById(R.id.update_stoke_edit);
                Button cancel=view.findViewById(R.id.ad_stoke_refuse);
                Button confirm=view.findViewById(R.id.ad_stoke_permit);

                builder.setView(view);
                alertDialog=builder.create();
                alertDialog.show();

                alertDialog.getWindow().setLayout(1000,500);


                // 设置取消
                cancel.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        alertDialog.dismiss();
                    }
                });
                // 设置确定
                confirm.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        // 另开线程，传输数据
                        new Thread()
                        {
                            @Override
                            public void run() {
                               try {
                                   // 收集数据
                                   long updateNumber=Long.parseLong(updateNmuberField.getText().toString());

                                   // 打包数据
                                   JSONObject object=new JSONObject();
                                   object.put("ISBN",ISBN);
                                   object.put("updateNumber",updateNumber);
                                   String toServer=String.valueOf(object);

                                   // 发送数据
                                   ServerMessage serverMessage = NetWorkUtils.getInstance().create(toServer,"/update_stoke");
                                   int code=serverMessage.getRespondCode();

                                   if(code==200)
                                   {
                                       // 发送成功
                                       Message message=new Message();
                                       message.what=UPDATE_STOKE_SUCCESS;
                                       handlerForUpdateBook.sendMessage(message);
                                   }
                                   else
                                   {
                                       // 失败
                                       Message message=new Message();
                                       message.what=UPDATE_STOKE_FAIL;
                                       handlerForUpdateBook.sendMessage(message);
                                   }
                               }catch (JSONException e){e.printStackTrace();}
                               catch (IOException e){e.printStackTrace(); // 失败
                                   Message message=new Message();
                                   message.what=UPDATE_STOKE_FAIL;
                                   handlerForUpdateBook.sendMessage(message);}
                            }
                        }.start();
                    }
                });

            }
        });
    }

    private void updateMessage() {
        new Thread()
        {
            @Override
            public void run() {
                try {
                    ServerMessage serverMessage= NetWorkUtils.getInstance().create(toServer,"/update_book_info");
                    int code=serverMessage.getRespondCode();
                    if(code==200)
                    {
                        Message message=new Message();
                        message.what=UPDATE_INFO_SUCCESS;
                        handlerForUpdateBook.sendMessage(message);
                    }
                    else
                    {
                        Message message=new Message();
                        message.what=UPDATE_INFO_FAIL;
                        handlerForUpdateBook.sendMessage(message);
                    }
                }catch (IOException e){e.printStackTrace();
                    Message message=new Message();
                    message.what=UPDATE_INFO_FAIL;
                    handlerForUpdateBook.sendMessage(message);}
                catch (JSONException e){e.printStackTrace();}
            }
        }.start();
    }

    private void packMessage() {
        try {
            JSONObject object=new JSONObject();
            object.put("ISBN",ISBN);
            object.put("bookName",bookName);
            object.put("press",press);
            object.put("author",author);
            object.put("introduction",introduction);
            object.put("type",type);
            toServer=String.valueOf(object);
        }catch (JSONException e){e.printStackTrace();}

    }

    private void collectMessage() {
        ISBN=ISBNField.getText().toString();
        bookName=bookNameField.getText().toString();
        press=pressField.getText().toString();
        author=authorField.getText().toString();
        type=typeField.getText().toString();
        introduction=introductionField.getText().toString();
    }

    private void setCancel() {
        cancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

    }

    private void initPage() {
        bookNameField=findViewById(R.id.ad_edit_book_name);
        ISBNField=findViewById(R.id.ad_edit_book_ISBN);
        authorField=findViewById(R.id.ad_edit_book_author);
        pressField=findViewById(R.id.ad_edit_book_press);
        sumNumberField=findViewById(R.id.ad_edit_book_sumNumber);
        nowNumberField=findViewById(R.id.ad_edit_book_nowNumber);
        typeField=findViewById(R.id.ad_edit_book_type);
        introductionField=findViewById(R.id.ad_edit_book_introduction);

        confirm=findViewById(R.id.ad_book_edit_confirm);
        cancel=findViewById(R.id.ad_book_edit_cancel);
        addStoke=findViewById(R.id.ad_book_stoke_edit);

        ISBN=getIntent().getExtras().getString("ISBN");
        bookName=getIntent().getExtras().getString("bookName");
        author=getIntent().getExtras().getString("author");
        press=getIntent().getExtras().getString("press");
        nowNumber=getIntent().getExtras().getLong("nowNumber");
        sumNumber=getIntent().getExtras().getLong("sumNumber");
        type=getIntent().getExtras().getString("type");
        introduction=getIntent().getExtras().getString("introduction");

        bookNameField.setText(bookName);
        ISBNField.setText(ISBN);
        authorField.setText(author);
        pressField.setText(press);
        nowNumberField.setText(nowNumber+"");
        sumNumberField.setText(sumNumber+"");
        typeField.setText(type);
        introductionField.setText(introduction);

    }
}